package com.example.administrator.moshuizhu.Activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.moshuizhu.Bean.Team;
import com.example.administrator.moshuizhu.Bean.Teams;
import com.example.administrator.moshuizhu.R;
import com.example.administrator.moshuizhu.Util.GetJsonUtil;
import com.google.gson.Gson;

import java.util.List;

public class TeamActivity extends AppCompatActivity {

    private TextView textView;
    private ListView listView;
    private List<Team.ItemBean> list;
    private List<Teams.ItemBean> list2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);
        initView();
        initData();
        initEvent();
    }
    private void initView(){
        textView = (TextView) findViewById(R.id.tv_1);
        listView = (ListView) findViewById(R.id.list_view);
    }
    private void initData(){
        String jsonTeam=new GetJsonUtil().getJson(TeamActivity.this,"Team.json");
        Gson gson=new Gson();
        Team team=gson.fromJson(jsonTeam,Team.class);
        list = team.getItem();
        String jsonTeams=new GetJsonUtil().getJson(TeamActivity.this,"Teams.json");
        Gson gson2=new Gson();
        Teams teams=gson.fromJson(jsonTeams,Teams.class);
        list2 = teams.getItem();
        MyAdapter myAdapter=new MyAdapter(TeamActivity.this, list);
        listView.setAdapter(myAdapter);
    }
    private void initEvent(){

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position<2){
                    Intent intent=new Intent(TeamActivity.this,TeamActivity.class);
                    intent.putExtra("id",list2.get(position).getTeamId());
                    intent.putExtra("pic",list2.get(position).getTeamPic());
                    intent.putExtra("winmatch",list2.get(position).getWinMatch());
                    intent.putExtra("losematch",list2.get(position).getLooseMatch());
                    intent.putExtra("pingmatch",list2.get(position).getPingMatch());
                    intent.putExtra("wingoal",list2.get(position).getWinGoal());
                    intent.putExtra("losegoal",list2.get(position).getLoseGoal());
                    intent.putExtra("score",list2.get(position).getScore());
                    intent.putExtra("detail",list2.get(position).getDetail());
                    startActivity(intent);
                }else {
                    Toast.makeText(TeamActivity.this, "数据获取失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    class MyAdapter extends BaseAdapter{
        private Context context;
        private List<Team.ItemBean> list3;

        public MyAdapter(Context context, List<Team.ItemBean> list) {
            this.context = context;
            this.list3 = list;
        }

        @Override
        public int getCount() {
            return list3.size();
        }
        @Override
        public Object getItem(int position) {
            return list3.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=View.inflate(TeamActivity.this,R.layout.item_activity,null);
            TextView textView1= (TextView) view.findViewById(R.id.team_id);
            TextView textView2= (TextView) view.findViewById(R.id.team_name);
            ImageView imageView= (ImageView) view.findViewById(R.id.team_iv);//绑定控件获取条目数和图片
            String pic=list3.get(position).getTeamPic();
            int mipmap=getResources().getIdentifier(pic,"mipmap",getPackageName());
            textView1.setText(list3.get(position).getTeamId()+"");
            textView2.setText(list3.get(position).getTeamName()+"");
            imageView.setImageResource(mipmap);
            return view;
        }
    }
}
